<?php 
/**
 * alltosun.com 补回数据
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2016-3-12 下午4:44:49 $
 * $Id: $
 */

class Action
{
    
    /**
     * 找回订单收货人收货电话和收货地址
     */
    public function back_order_data()
    {
//         $order_list = _model('order')->getList(array(1=>1));
//         if ($order_list) {
//             foreach ($order_list as $k => $v) {
//                 if ((!$v['mobile'] || !$v['receiver']) && !$v['agency_id']) {
//                     $user_info = user_helper::get_user_info($v['user_id']);
//                     if (!$user_info) {
//                         continue;
//                     }
//                     $role_id = user_helper::get_user_role_id($user_info['id']);
//                     if ($role_id != user_config::AGENCY) {
//                         continue;
//                     }
//                     $realname = $user_info['realname'] ? $user_info['realname'] : $user_info['nickname'];
//                     _model('order')->update($v['id'], array('receiver'=>$realname, 'mobile'=>$user_info['mobile']));
//                 }
//             }
//         }
    }
    
    /**
     * 更改经销商的助理
     */
    public function back_dealer_assistant_data()
    {
        $mobile = Request::getParam('old_assistant_mobile', '15257549172');
        $new_mobile = Request::getParam('new_assistant_mobile', '18205812780');
        $old_user_info = user_helper::get_user_info_by_mobile($mobile);
        $old_id = $old_user_info['id'];
        $new_user_info = user_helper::get_user_info_by_mobile($new_mobile);
        $new_id = $new_user_info['id'];
        _model('user')->update(array('assistant_user_id'=>$old_id), array('assistant_user_id'=>$new_id));
        
        echo '更新成功';
    }
    
    // 开始时间 2016-02-18 15:55
    public function back_data()
    {
//         $set_up_time = '2016-02-18 15:55:00';
        $set_up_time = '2016-02-18 23:59:59';
        
        $before_date = date('');
        
        $time = strtotime($set_up_time);
        for ($i=0, $cur_time = $time; date('Ymd', $cur_time) < date('Ymd', strtotime('-1 day')); $i++) {
            $cur_time = strtotime('+'.$i.' day', $time);
            $data_list = $this->get_admin_data($cur_time);
            $this->save_admin_data($data_list, $cur_time);
        }   
    }
    
    /**
     * 按照时间返回对应的日，周，月，统计用
     * @param unknown_type $type
     * @param unknown_type $time
     * @return string
     */
    private function get_date($type, $time)
    {
        if (!$time)  $time = time();

        if ($type == 'day') {
            $date = date('Y-m-d', $time);
        } else if ($type == 'week') {
            // 第几周
            $date = strftime('%Y%W', $time);
        } else if ($type == 'month') {
            $date = date('Ym', $time);
        }
        return $date;
    }
    
    /**
     * 保存记录
     */
    private function save_admin_data($data_list, $time = '')
    {
        if (!$time)  $time = time();
        if (!$data_list) {
            return false;
        }
        $list = array();
        $list['dealer'] = array();
        $list['store'] = array();
        
        $data_list['dealer_data']['add_time'] = date('Y-m-d H:i:s', $time);
        $data_list['store_data']['add_time'] = date('Y-m-d H:i:s', $time);
        $day_time = $this->get_date('day', $time);
        $week_time = $this->get_date('week', $time);
        $month_time = $this->get_date('month', $time);
        
        // 记录经销商相关统计数据
        $list['dealer'] = array();
        $list['dealer']['day']   = $data_list['dealer_data'];
        $list['dealer']['week']  = $data_list['dealer_data'];
        $list['dealer']['month'] = $data_list['dealer_data'];
        $list['dealer']['day']['date']   = $day_time;
        $list['dealer']['week']['date']  = $week_time;
        $list['dealer']['month']['date'] = $month_time;
        
        // 记录店铺相关统计数据
        $list['store'] = array();
        $list['store']['day']   = $data_list['store_data'];
        $list['store']['week']  = $data_list['store_data'];
        $list['store']['month'] = $data_list['store_data'];
        $list['store']['day']['date']   = $day_time;
        $list['store']['week']['date']  = $week_time;
        $list['store']['month']['date'] = $month_time;

        foreach ($list as $key => $data) {
            foreach ($data as $k => $v) {
                $cur_date = $this->get_date($k, $time);
                $last_info = _model('admin_'.$key.'_data_'.$k)->read(array('date'=>$cur_date), 'ORDER BY `add_time` DESC');
                echo '<pre>';
                print_r($last_info);
                if ($last_info && $cur_date == $v['date']) {
                    echo '更新了','<br/>';
                    _model('admin_'.$key.'_data_'.$k)->update($last_info['id'], $v);
                } else {
                    echo '添加了','<br/>';
                    _model('admin_'.$key.'_data_'.$k)->create($v);
                }
            }
        }
        
    }
    
    /**
     * 获取指定一天的统计数据
     * @param unknown $time 时间戳
     * @return number
     */
    public function get_admin_data($time = '')
    {
        if (!$time)  $time = time();
        
        $admin_data_list = array();
        
        // 经销商相关数据
        // 经销商数量
        $admin_data_list['dealer_data']['dealer_num'] = user_helper::get_user_num_by_role_id(user_config::AGENCY, array('`add_time` <= '=>date('Y-m-d', $time).' 23:59:59'));
        
        // 今日新增：多少人增长率多少
        $today_add_dealer_num = user_helper::get_user_num_by_role_id(user_config::AGENCY, array('`add_time` LIKE '=>date('Y-m-d', $time).'%'));
        $before_dealer_num    = user_helper::get_user_num_by_role_id(user_config::AGENCY, array('`add_time` < '=>date('Y-m-d', $time).' 00:00:00'));
        
        $admin_data_list['dealer_data']['today_add_dealer_num'] = $today_add_dealer_num;
        $admin_data_list['dealer_data']['dealer_growth_rate']   = round(($today_add_dealer_num / $before_dealer_num) * 100, 2);
        
        // 成交订单数/已收到款项
        $admin_data_list['dealer_data']['dealer_order_num']   = order_helper::get_order_num_by_progress(array(3,5, 10), 1, array('is_enable'=>1, 'status'=>1, '`add_time` <= '=>date('Y-m-d', $time).' 23:59:59'));
        $admin_data_list['dealer_data']['dealer_order_money'] = order_helper::get_order_money_by_progress(array(3,5, 10), 1,  array('is_enable'=>1, 'status'=>1, '`add_time` <= '=>date('Y-m-d', $time).' 23:59:59'));
        
        // 已成功出货：多少箱 (总部向合伙人发货)
        $admin_data_list['dealer_data']['delivery_goods_num'] = order_helper::get_order_goods_num(array(5, 10), 1, array('is_enable'=>1, 'status'=>1, '`add_time` <= '=>date('Y-m-d', $time).' 23:59:59'));
        
        // 待收款订单/等待收款项：订单数量/（未成交金额）多少元  @FIXME
        $admin_data_list['dealer_data']['prepare_dealer_order_num']   = order_helper::get_order_num_by_progress(1, 1, array('is_enable'=>1, 'status'=>1, '`add_time` <= '=>date('Y-m-d', $time).' 23:59:59'));
        $admin_data_list['dealer_data']['prepare_dealer_order_money'] = order_helper::get_order_money_by_progress(1, 1, array('is_enable'=>1, 'status'=>1, '`add_time` <= '=>date('Y-m-d', $time).' 23:59:59'));
        
        // 等待发货：多少箱 (只要提交订单就算是) @FIXME
        $admin_data_list['dealer_data']['prepare_delivery_order_num'] = order_helper::get_order_goods_num(array(1, 3), 1, array('is_enable'=>1, 'status'=>1, '`add_time` <= '=>date('Y-m-d', $time).' 23:59:59'));
        
        // 合伙人总库存：多少箱
        $dealer_ids = user_helper::get_role_user_ids(user_config::AGENCY, '' , array('is_enable'=>1, '`add_time` <= '=>date('Y-m-d', $time).' 23:59:59'));
        $admin_data_list['dealer_data']['all_dealer_stock'] = dealers_helper::get_dealer_stock($dealer_ids);
        
        // 合伙人总员工：多少人 (市场人员的总数量)
        $store_list = store_helper::get_store_list_by_dealer_id($dealer_ids, array('is_enable'=>1, 'is_delete'=>1, '`add_time` <= '=>date('Y-m-d', $time).' 23:59:59' ));
        $marketing_ids = array();
        if ($store_list) {
            foreach ($store_list as $k=>$v) {
                if (!in_array($v['marketing_id'], $marketing_ids) && $v['marketing_id'] != $v['agency_id'] && $v['marketing_id'] != 0) {
                    array_push($marketing_ids, $v['marketing_id']);
                }
            }
        }
        $admin_data_list['dealer_data']['marketing_num'] = count(array_unique($marketing_ids));
        
        // 终端店铺相关数据(分店)
        $admin_data_list['store_data']['store_num'] = store_helper::get_all_store_num(array('`add_time` <= '=>date('Y-m-d', $time).' 23:59:59'));
        // 今日新增：多少门店增长率多少
        $today_add_store_num = user_helper::get_user_num_by_role_id(user_config::STORE_MANAGER, array('`add_time` LIKE '=>date('Y-m-d', $time).'%'));
        $admin_data_list['store_data']['today_add_store_num'] = $today_add_store_num;
        $before_store_num = user_helper::get_user_num_by_role_id(user_config::STORE_MANAGER, array('`add_time` < '=>date('Y-m-d', $time).' 00:00:00'));
        $admin_data_list['store_data']['store_growth_rate'] = round($today_add_store_num / $before_store_num * 100, 2);
        // 已进货门店数量：(必须是确认收货的门店)
        $admin_data_list['store_data']['is_stock_store_num'] = user_helper::get_user_num_by_role_id(user_config::STORE_MANAGER, array('`score` > '=>0, '`add_time` <= '=>date('Y-m-d', $time).' 23:59:59'));
        // 未进货门店数量：(其他)
        $admin_data_list['store_data']['not_stock_store_num'] = $admin_data_list['store_data']['store_num'] - $admin_data_list['store_data']['is_stock_store_num'];
        // 门店订单总量：(所有订单)
        $admin_data_list['store_data']['store_order_num'] = order_helper::get_order_num_by_progress(array(1, 3, 5, 10, 15), 5, array('status'=>1, 'is_enable'=>1, '`add_time` <= '=>date('Y-m-d', $time).' 23:59:59'));
        // 今日新增订单量：(统计处在配送中的订单,不会撤销的订单)
        $admin_data_list['store_data']['store_new_order_num'] = order_helper::get_order_num_by_progress(array(5, 10), 5, array('status'=>1, 'is_enable'=>1, '`add_time` LIKE '=>date('Y-m-d', $time).'%'));
        
        return $admin_data_list;
    }

    public function recover_order_dealer_address()
    {
        $dealer_ids = user_helper::get_role_user_ids(user_config::AGENCY, '');
        if ($dealer_ids) {
            foreach ($dealer_ids as $k => $v) {
                $dealer_user_info = user_helper::get_user_info($v);
                if ($dealer_user_info) {
                    _model('order')->update(array('user_id'=>$v, 'dealer_address'=>''), array('dealer_address'=> $dealer_user_info['store_house']));
                }
            }
        }
        
        echo '更新成功!';
    }
    
    
    
}


?>